import utils from '../../utils/utils'
import * as api from '../../utils/api'
// import axios from 'axios'
import axios from '../../utils/http'

const state = {
  user: utils.getStorage('user') || {}
};
const actions = {
  login({commit}, obj) {
    // console.log('action')
    // console.log(obj.name)
    return axios({
      url: api.GET_LOGIN,
      method: 'get',
      params: {
        user_phone:obj.name,
        user_pwd:obj.pwd,
        remember_pwd:obj.remember
      }
    }).then(data => {
      if (data.data.code === 200) {
        //调用mutations里面的addUser方法
        commit('addUser', data.data.data[0])
        this.dengluzhexinxi = data.data.data[0]
        //将登陆者信息存到session里面
        utils.setStorage('user', this.dengluzhexinxi)
      }
      return(data)
    })
  },
  outlogin({commit}){
    commit('removeUser')
  }
};
/* 函数 */
const mutations = {
  addUser(state, user) {
    state.user = user
  },
  removeUser(state){
    state.user = ''
    utils.removeStorage('token')
  }
};
/* 计算 */
const getters = {

};
export default {
  state,
  actions,
  mutations,
  getters
}